/*
 * @Description: 
 * @Author: nardy.sanghx
 * @Date: 2019-05-22 19:33:14
 * @LastEditors: nardy.sanghx
 * @LastEditTime: 2019-05-28 17:07:25
 */
const express = require("express");
const router = express.Router();
const passport = require("passport");
const Ipfont = require('../../models/Ipfont');

// $route POST api/Ipfont/add
// @desc  创建新的
// @access Privata
router.post("/add", (req, res) => {
    // 查询数据库中是否存在
    Ipfont.findOne({ip:req.body.ip,dataId:req.body.dataId})
          .then((ipfont)=> {
              if(ipfont) {
                  return res.json({code:1,msg:'已经存在'})
              }else{
                const newData = new Ipfont({
                    ip: req.body.ip,
                    dataId: req.body.dataId,
                    date: req.body.date
                })
                newData.save()
                    .then(ipfont => res.json(ipfont))
                    .catch(err => console.log(err))
              }
          })
    
})

// $route GET api/Ipfont/:id
// @desc  获取单个
// @access Privata
//  passport.authenticate("jwt", { session: false }), 
router.get("/:id",(req, res) => {
    Ipfont.findOne({ ip: req.params.ip })
        .then(Ipfont => {
            if (!Ipfont) {
                return res.status(404).json("还没有数据哦");
            }
            res.json(Ipfont);
        })
        .catch(err => res.status(403).json(err))
})


// $route delete api/data/del/:id
// @desc  删除
// @access Privata
router.delete("/del/:id", passport.authenticate("jwt", { session: false }), (req, res) => {
    Ipfont.findOneAndRemove({_id: req.params.id}).then(Ipfont =>{
        Ipfont.save().then(Ipfont => res.json(Ipfont))
    }).catch(err => res.status(404).json(err))
})

module.exports = router;